<template>
<div class="order">
  <van-nav-bar style="margin-bottom:0;" title="积分订单确认" left-arrow @click-left="goback"/>
  <van-cell-group>
      <van-cell v-if="checkedAddress" isLink @click="goAddressList()" title="收货地址">
      <div slot="label">
        <div>
         <span>{{ checkedAddress.name }} </span>
         <span>{{ checkedAddress.tel }} </span>
      </div>
      <div>
        {{ checkedAddress.addressDetail }}
      </div>
      </div>
    </van-cell>
  </van-cell-group>

    <van-card
      v-for="item in checkedGoodsList"
      :key="item.id"
      :title="item.name"
      :num="item.number"
      :price="item.point"
      :thumb="item.img"
    >
    </van-card>

    <van-submit-bar
      :price="actualPrice*100"
      label="总计："
      buttonText="支付"
      :disabled="isDisabled"
      @submit="onSubmit"
    />
</div>
</template>

<script>
import { Card, Tag, ard, Field, SubmitBar, Toast, NavBar, RadioGroup, Radio, Row, Col } from 'vant';
import { CouponCell, CouponList, Popup } from 'vant';
import { preExchange, orderSubmit, couponSelectList, orderAlipayHbInfo, orderAlipay, orderPrepay, getUserOrderStatus, exchangeSave } from '@/api/api';
import { getLocalStorage, setLocalStorage } from '@/utils/local-storage';
import dayjs from 'dayjs';

export default {
  data() {
    return {
      eyu: true,
      eyu1: '',
      eyu2: '',
      checkedGoodsList: [],
      checkedAddress: {},
      availableCouponLength: 0, // 可用的优惠券数量
      goodsTotalPrice: 0, //商品总价
      balance: 0, //可用余额
      freightPrice: 0, //快递费
      couponPrice: 0, //优惠券的价格
      grouponPrice: 0, //团购优惠价格
      orderTotalPrice: 0, //订单总价
      actualPrice: 0, //实际需要支付的总价
      message: '',
      yue: '',
      huabei: [],
      huabeiShow: false,
      huabeiSel: 3,
      radio: '微信支付',
      isDisabled: false,
      showList: false,
      chosenCoupon: -1,
      coupons: [],
      disabledCoupons: [],
      isMj: false
    };
  },
  created() {
    this.init();
  },

  methods: {
    onSubmit() {
      const {AddressId, intdata} = getLocalStorage('AddressId', 'intdata',);
      if (AddressId === null || AddressId === '0' || AddressId === 0) {
        Toast.fail('请设置收货地址');
        return;
      }
      this.isDisabled = true;
      var intdataItem = JSON.parse(intdata)
      exchangeSave({addressId: AddressId, strObj: intdataItem}).then(res => {
        console.log(res)
        // 下单成功，重置下单参数。
        if (res.data.errno === 0) {
          Toast('下单成功')
          this.$router.push('/user')
        }
       
      }).catch(error => {
        this.isDisabled = false;
        this.$toast("下单失败");
      })

    },
    delayer (action, delay = 600) {
      let timer = -1;
      return nv => {
        clearTimeout(timer);
        timer = setTimeout(() => {
          action(nv);
        }, delay);
      };
    },
    goAddressList() {
      this.$router.push({
        path: '/user/address'
      });
    },
    getCouponValue() {
      if(this.couponPrice !== 0 ){
        return "-¥" + this.couponPrice + ".00元"
      }
      if(this.availableCouponLength !== 0){
        return this.availableCouponLength + "张可用"
      }
      return '没有可用优惠券'
    },
    goback() {
      this.$router.go(-1);
    },
    getCoupons() {
      const {AddressId, CartId, CouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId');
      couponSelectList({cartId: CartId, grouponRulesId: 0}).then(res => {
        var cList = res.data.data
        this.coupons = []
        for(var i = 0; i < cList.length; i++){
          var c = cList[i]

          var coupon = {
            id: c.id,
            name: c.name,
            condition: c.min,
            value: c.discount * 100,
            description: c.desc,
            startAt: new Date(c.startTime).getTime()/1000,
            endAt: new Date(c.endTime).getTime()/1000,
            valueDesc: c.discount,
            unitDesc: '元'            
          }
          this.coupons.push(coupon)

          if(c.id === this.couponId){
            this.chosenCoupon = i;
            break;
          }
        }
        
        this.showList = true
      })
    },
    init() {
      // const {AddressId, CartId, CouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId');
      // console.log(AddressId, CartId, CouponId)
      // cartCheckout({cartId: CartId, addressId: AddressId, couponId: CouponId, grouponRulesId: 0}).then(res => {
      //     var data = res.data.data
      //     console.log(data.checkedGoodsList)

      //     this.checkedGoodsList = data.checkedGoodsList;
      //     this.checkedAddress= data.checkedAddress;
      //     this.availableCouponLength= data.availableCouponLength;
      //     this.actualPrice= data.actualPrice;
      //     this.couponPrice= data.couponPrice;
      //     this.grouponPrice= data.grouponPrice;
      //     this.freightPrice= data.freightPrice;
      //     this.goodsTotalPrice= data.goodsTotalPrice;
      //     this.orderTotalPrice= data.orderTotalPrice;
      //     this.balance= data.balance;
      //     if (data.userLevel === 2) {
      //       // this.eyu = true
      //     }

      //     setLocalStorage({AddressId: data.addressId, CartId: data.cartId, CouponId: data.couponId});

      //     getUserOrderStatus().then(e => {
      //       // console.log(e, '123333')
      //       if (this.actualPrice > 1000 && data.userLevel === 2) {
      //         this.eyu = e.data.data
      //         this.actualPrice = this.actualPrice - parseInt((this.actualPrice / 1000) * 50)
      //         // console.log(parseInt((this.actualPrice / 1000) * 50), '333')
      //       }
      //     })
      // });
      const {AddressId, orderTotalPoint, intdata} = getLocalStorage('AddressId', 'orderTotalPoint', 'intdata');
      if (orderTotalPoint) {
        var intdataItem = JSON.parse(intdata)
        console.log(intdataItem, 'c1')
        console.log(AddressId)
        preExchange({addressId: AddressId,strObj: intdataItem}).then(res => {
          var data = res.data.data
          console.log(data, 'pre')
          this.checkedGoodsList = data.goodsData;
          this.checkedAddress= data.checkedAddress;
          this.actualPrice= data.orderTotalPoint;
          setLocalStorage({AddressId: data.addressId});
        })
        // this.checkedGoodsList = data.goodsData;
        // this.checkedAddress= data.checkedAddress;
        // this.actualPrice= data.orderTotalPoint;
      }
    },
    onChange(index) {
      this.showList = false;
      this.chosenCoupon = index;
      
      if(index === -1 ){
        setLocalStorage({CouponId: -1});
      }
      else{
        const couponId = this.coupons[index].id;
        setLocalStorage({CouponId: couponId});  
      }

      this.init()
    },
    onExchange() {
      this.$toast("兑换暂不支持");
    }    
  },

  components: {
    [Row.name]: Row ,
    [Col.name]: Col ,
    [RadioGroup.name]: RadioGroup ,
    [Radio.name]: Radio ,
    [Toast.name]: Toast ,
    [NavBar.name]: NavBar ,
    [SubmitBar.name]: SubmitBar,
    [Card.name]: Card,
    [Field.name]: Field,
    [Tag.name]: Tag,
    [CouponCell.name]: CouponCell,
    [CouponList.name]: CouponList,
    [Popup.name]: Popup
  }
};
</script>


<style lang="scss" scoped>
.order-coupon {
  margin-top: 10px;
}
.pay-icon {
  width: 25px;
}
/deep/.pay-list {
  .van-cell__value {
    position: absolute;
    left: 12px;
  }
  .van-cell__title {
    padding-left: 27px;
  }
}
.huabei {
  border: 1px solid #ccc;
  margin: -1px 0 0 -1px;
  font-size: 12px;
  z-index: 1;
  .fenqi span{
    font-size: 14px;
    color: #ff6600;
    margin-right: 5px;
  }
  .tit span{
    font-size: 18px;
    color: #ff6600;
    margin-right: 5px;
  }
  .tit2 {
    padding-bottom: 5px;
    margin-bottom: 5px;
    border-bottom: 1px solid #ddd;
  }
}
.selected {
  border:1px solid #ff6600;
  transform: translateX(0);
  z-index: 3;
  background: transparent url(http://www.dtw360.cn/plugs/ui-choose/src/icon-selected.png) no-repeat right bottom;
}
</style>